//
//  RootViewController.m
//  分享
//
//  Created by lanouhn on 15/6/10.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

@implementation RootViewController

- (void)SubView{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.tag = 110;
    imageView.frame = CGRectMake(100, 70, 150, 200);
    imageView.backgroundColor = [UIColor redColor];

    UIButton *picButton = [UIButton buttonWithType:UIButtonTypeCustom];
    picButton.frame = CGRectMake(120, 300, 100, 30);
    picButton.backgroundColor = [UIColor blueColor];
    [picButton setTitle:@"打开相册" forState:UIControlStateNormal];
    [picButton addTarget:self action:@selector(openPic:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cameraBtn.frame = CGRectMake(120, 430, 100, 30);
    cameraBtn.backgroundColor = [UIColor blackColor];
    [cameraBtn setTitle:@"打开相机" forState:UIControlStateNormal];
    [cameraBtn addTarget:self action:@selector(openCam:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cameraBtn];
    [self.view addSubview:imageView];
    [self.view addSubview:picButton];
}

- (void)openPic:(UIButton *)button{
    UIImagePickerController *pickController = [[UIImagePickerController alloc] init];
    pickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pickController.delegate = self;
    [self presentViewController:pickController animated:YES completion:^{
        
    }];
    
}

#pragma mark ---------------
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:110];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    imageView.image = image;
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

//当用户取消相册时，调用的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];//以模态的方式弹出视图
}

- (void)openCam:(UIButton *)sender{
    //判断摄像头是否可用
    BOOL isRearCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];//判断后置摄像头是否可用
    BOOL isfrontCamera = [UIImagePickerController isCameraDeviceAvailable:(UIImagePickerControllerCameraDeviceFront)];//判断前置摄像头是否可用
    
    if (!isRearCamera) {
        NSLog(@"后置摄像头不可用");
       

    }
    if (!isfrontCamera) {
        NSLog(@"前置摄像头不可用");
        return;//不能省略；
    }
    
    //初始化图片选择控制器
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;//通过照相来选中相片
    imagePicker.allowsEditing = YES;//设置拍照时下方工具栏是否显示，如果需要自定义拍摄界面可以把工具栏隐藏
    imagePicker.delegate = self;//服从协议
    [self presentViewController:imagePicker animated:YES completion:nil];
//    [imagePicker dismissViewControllerAnimated:YES completion:NULL];以模态的方法隐藏视图
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self SubView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
